<?php
/**
 * 会员分组
 * @author hushuai
 *
 */
class GroupAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
 	/**
     * 添加会员分组级
     */
    public function add(){
    	
    	parent::do_add("Group");
    
    }
    
    /**
     * 添加组内成员
     */
    public function addMem(){
    	
    	$idStr   = $this->_get("idStr",  "trim","");
    	$groupid = $this->_get("groupid","intval",0);
    	$map     = array("idStr"=>$idStr,"groupid"=>$groupid);
    	$this->success(D('MemberGroup')->addMem($map));
    	
    }
    
    /**
     * 会员分组列表
     * @param int isdel 删除标志位
     * return array
     */
    public function index(){
    	
    	$map['isdel'] = $this->_get("isdel","intval",2);
     	
    	$model = D('Group');
    	$data  = $model->listAll($map);
    	
    	$this->success($data);
    }
    
    /**
     * 修改会员分组
     */
    public function edit(){
    	
    	if(IS_GET){
    		
    		$id = $this->_get("id",  "intval",0);
    		$this->success(D('Group')->find($id));
    		
    	}else{
    
    		parent::do_edit("Group");
    	}
    
    }
    
    /**
     * 查询组内成员
     * @access public
     * @param string $where    查询条件
     * @param string $desc     降序排列
     * @param string $order    排序方式
     * @param string $pageNum  每页数量
     * @param string $nowPage  当前页数
     * @return array
     */
    public function groupMember(){
    	
    		$desc     = $this->_req("orderDirection","trim","desc");;
    		$order    = $this->_req("orderName","trim","addtime");
    		$pageNum  = $this->_req("pageNum","intval",C('PAGE_NUMS'));
    		$nowPage  = $this->_req("p","intval",1);
    		$groupid  = $this->_req("groupid","intval",0);
    		$con      = $order.' '.$desc;
    		$table    = "MemberMemberGroupView";
    		
    		$map['isdel']   = 2;
    		$map['groupid'] = $groupid;
    		
    		$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$con));
    
    }
    
    /**
     * 删除会员分组
     */
    public function del(){
    	
    	parent::do_del("Group",true);   //删除会员等级表中相关数据
    	//parent::do_del("MemberGroup",true);	  //删除会员等级表中相关数据
    	
    }

    /**
     * 删除会员会员分组
     */
    public function memberGroupDel(){
   		
		$del['groupid'] = $this->_get("groupid","intval",0);
		$customstr      = $this->_get("customid","trim","");
		$idArr          = explode(',',$customstr);
		$model = D('MemberGroup');
		foreach ($idArr as $k){
			$del['customid'] = $k;
			$res = $model->where($del)->delete();
		}
       
    }
    
    /**
     * 根据分组编号查询组内成员
     */
    public function conSelect(){
    	
       $id = $this->_get("id","intval",0);
       
       $this->success(M("MemberGroup")->where("groupid=".$id)->select());
    }
    
    /**
     * 移动会员分组
     * @param  $id      int   会员卡关用户关联表主键
     * @param  $vipid   int   会员卡编号
     * return  boolean
     */
    public function move(){
    	
    	$map['id']         = $this->_get("id", "trim","");
    	$map['groupid']    = $this->_get("groupid",  "intval",0);
    	 
    	$model = D('Group');
    	$flag  = $model->move($map);
    	if($flag){
    		$this->success("调整成功！");
    	}else{
    		$this->error("调整失败！");
    	}
    }
    
}